home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / elispman.lha / elispman / variables.texi < prev    next >
Lisp/Scheme  |  1993-05-19  |  43KB  |  1,231 lines

  1. @c -*-texinfo-*-
  2. @c This is part of the GNU Emacs Lisp Reference Manual.
  3. @c Copyright (C) 1990, 1991, 1992, 1993 Free Software Foundation, Inc. 
  4. @c See the file elisp.texi for copying conditions.
  5. @setfilename ../info/variables
  6. @node Variables, Functions, Control Structures, Top
  7. @chapter Variables
  8. @cindex variable
  9.  
  10.   A @dfn{variable} is a name used in a program to stand for a value.
  11. Nearly all programming languages have variables of some sort.  In the
  12. text for a Lisp program, variables are written using the syntax for
  13. symbols.
  14.  
  15.   In Lisp, unlike most programming languages, programs are represented
  16. primarily as Lisp objects and only secondarily as text.  The Lisp
  17. objects used for variables are symbols: the symbol name is the variable
  18. name, and the variable's value is stored in the value cell of the
  19. symbol.  The use of a symbol as a variable is independent of whether
  20. the same symbol has a function definition.  @xref{Symbol Components}.
  21.  
  22.   The textual form of a program is determined by its Lisp object
  23. representation; it is the read syntax for the Lisp object which
  24. constitutes the program.  This is why a variable in a textual Lisp
  25. program is written as the read syntax for the symbol that represents the
  26. variable.
  27.  
  28. @menu
  29. * Global Variables::      Variable values that exist permanently, everywhere.
  30. * Constant Variables::    Certain "variables" have values that never change.
  31. * Local Variables::       Variable values that exist only temporarily.
  32. * Void Variables::        Symbols that lack values.
  33. * Defining Variables::    A definition says a symbol is used as a variable.
  34. * Accessing Variables::   Examining values of variables whose names
  35.                             are known only at run time.
  36. * Setting Variables::     Storing new values in variables.
  37. * Variable Scoping::      How Lisp chooses among local and global values.
  38. * Buffer-Local Variables::  Variable values in effect only in one buffer.
  39. @end menu
  40.  
  41. @node Global Variables, Constant Variables, Variables, Variables
  42. @section Global Variables
  43. @cindex global variable
  44.  
  45.   The simplest way to use a variable is @dfn{globally}.  This means that
  46. the variable has just one value at a time, and this value is in effect
  47. (at least for the moment) throughout the Lisp system.  The value remains
  48. in effect until you specify a new one.  When a new value replaces the
  49. old one, no trace of the old value remains in the variable.
  50.  
  51.   You specify a value for a symbol with @code{setq}.  For example,
  52.  
  53. @example
  54. (setq x '(a b))
  55. @end example
  56.  
  57. @noindent
  58. gives the variable @code{x} the value @code{(a b)}.  Note that the
  59. first argument of @code{setq}, the name of the variable, is not
  60. evaluated, but the second argument, the desired value, is evaluated
  61. normally.
  62.  
  63.   Once the variable has a value, you can refer to it by using the symbol
  64. by itself as an expression.  Thus,
  65.  
  66. @example
  67. @group
  68. x
  69.      @result{} (a b)
  70. @end group
  71. @end example
  72.  
  73. @noindent
  74. assuming the @code{setq} form shown above has already been executed.
  75.  
  76.   If you do another @code{setq}, the new value replaces the old one:
  77.  
  78. @example
  79. @group
  80. x
  81.      @result{} (a b)
  82. @end group
  83. @group
  84. (setq x 4)
  85.      @result{} 4
  86. x
  87.      @result{} 4
  88. @end group
  89. @end example
  90.  
  91. @node Constant Variables, Local Variables, Global Variables, Variables
  92. @section Variables that Never Change
  93. @vindex nil
  94. @vindex t
  95. @kindex setting-constant
  96.  
  97.   Emacs Lisp has two special symbols, @code{nil} and @code{t}, that
  98. always evaluate to themselves.  These symbols cannot be rebound, nor can
  99. their value cells be changed.  An attempt to change the value of
  100. @code{nil} or @code{t} signals a @code{setting-constant} error.
  101.  
  102. @example
  103. @group
  104. nil @equiv{} 'nil
  105.      @result{} nil
  106. @end group
  107. @group
  108. (setq nil 500)
  109. @error{} Attempt to set constant symbol: nil
  110. @end group
  111. @end example
  112.  
  113. @node Local Variables, Void Variables, Constant Variables, Variables
  114. @section Local Variables
  115. @cindex binding local variables
  116. @cindex local variables
  117. @cindex local binding
  118. @cindex global binding
  119.  
  120.   Global variables are given values that last until explicitly
  121. superseded with new values.  Sometimes it is useful to create variable
  122. values that exist temporarily---only while within a certain part of the
  123. program.  These values are called @dfn{local}, and the variables so used
  124. are called @dfn{local variables}.
  125.  
  126.   For example, when a function is called, its argument variables receive
  127. new local values which last until the function exits.  Similarly, the
  128. @code{let} special form explicitly establishes new local values for
  129. specified variables; these last until exit from the @code{let} form.
  130.  
  131. @cindex shadowing of variables
  132.   When a local value is established, the previous value (or lack of one)
  133. of the variable is saved away.  When the life span of the local value is
  134. over, the previous value is restored.  In the mean time, we say that the
  135. previous value is @dfn{shadowed} and @dfn{not visible}.  Both global and
  136. local values may be shadowed (@pxref{Scope}).
  137.  
  138.   If you set a variable (such as with @code{setq}) while it is local,
  139. this replaces the local value; it does not alter the global value, or
  140. previous local values that are shadowed.  To model this behavior, we
  141. speak of a @dfn{local binding} of the variable as well as a local value.
  142.  
  143.   The local binding is a conceptual place that holds a local value.
  144. Entry to a function, or a special form such as @code{let}, creates the
  145. local binding; exit from the function or from the @code{let} removes the
  146. local binding.  As long as the local binding lasts, the variable's value
  147. is stored within it.  Use of @code{setq} or @code{set} while there is a
  148. local binding stores a different value into the local binding; it does
  149. not create a new binding.
  150.  
  151.   We also speak of the @dfn{global binding}, which is where
  152. (conceptually) the global value is kept.
  153.  
  154. @cindex current binding
  155.   A variable can have more than one local binding at a time (for
  156. example, if there are nested @code{let} forms that bind it).  In such a
  157. case, the most recently created local binding that still exists is the
  158. @dfn{current binding} of the variable.  (This is called @dfn{dynamic
  159. scoping}; see @ref{Variable Scoping}.)  If there are no local bindings,
  160. the variable's global binding is its current binding.  We also call the
  161. current binding the @dfn{most-local existing binding}, for emphasis.
  162. Ordinary evaluation of a symbol always returns the value of its current
  163. binding.
  164.  
  165.   The special forms @code{let} and @code{let*} exist to create
  166. local bindings.
  167.  
  168. @defspec let (bindings@dots{}) forms@dots{}
  169. This function binds variables according to @var{bindings} and then
  170. evaluates all of the @var{forms} in textual order.  The @code{let}-form
  171. returns the value of the last form in @var{forms}.
  172.  
  173. Each of the @var{bindings} is either @w{(i) a} symbol, in which case
  174. that symbol is bound to @code{nil}; or @w{(ii) a} list of the form
  175. @code{(@var{symbol} @var{value-form})}, in which case @var{symbol} is
  176. bound to the result of evaluating @var{value-form}.  If @var{value-form}
  177. is omitted, @code{nil} is used.
  178.  
  179. All of the @var{value-form}s in @var{bindings} are evaluated in the
  180. order they appear and @emph{before} any of the symbols are bound.  Here
  181. is an example of this: @code{Z} is bound to the old value of @code{Y},
  182. which is 2, not the new value, 1.
  183.  
  184. @example
  185. @group
  186. (setq Y 2)
  187.      @result{} 2
  188. @end group
  189. @group
  190. (let ((Y 1) 
  191.       (Z Y))
  192.   (list Y Z))
  193.      @result{} (1 2)
  194. @end group
  195. @end example
  196. @end defspec
  197.  
  198. @defspec let* (bindings@dots{}) forms@dots{}
  199. This special form is like @code{let}, except that each symbol in
  200. @var{bindings} is bound as soon as its new value is computed, before the
  201. computation of the values of the following local bindings.  Therefore,
  202. an expression in @var{bindings} may reasonably refer to the preceding
  203. symbols bound in this @code{let*} form.  Compare the following example
  204. with the example above for @code{let}.
  205.  
  206. @example
  207. @group
  208. (setq Y 2)
  209.      @result{} 2
  210. @end group
  211. @group
  212. (let* ((Y 1)
  213.        (Z Y))    ; @r{Use the just-established value of @code{Y}.}
  214.   (list Y Z))
  215.      @result{} (1 1)
  216. @end group
  217. @end example
  218. @end defspec
  219.  
  220.   Here is a complete list of the other facilities which create local
  221. bindings:
  222.  
  223. @itemize @bullet
  224. @item
  225. Function calls (@pxref{Functions}).
  226.  
  227. @item
  228. Macro calls (@pxref{Macros}).
  229.  
  230. @item
  231. @code{condition-case} (@pxref{Errors}).
  232. @end itemize
  233.  
  234. @defvar max-specpdl-size
  235. @cindex variable limit error
  236. @cindex evaluation error
  237. @cindex infinite recursion
  238.   This variable defines the limit on the total number of local variable
  239. bindings and @code{unwind-protect} cleanups (@pxref{Nonlocal Exits})
  240. that are allowed before signaling an error (with data @code{"Variable
  241. binding depth exceeds max-specpdl-size"}).
  242.  
  243.   This limit, with the associated error when it is exceeded, is one way
  244. that Lisp avoids infinite recursion on an ill-defined function.
  245.  
  246.   The default value is 600.
  247.  
  248.   @code{max-lisp-eval-depth} provides another limit on depth of nesting.
  249. @xref{Eval}.
  250. @end defvar
  251.  
  252. @node Void Variables, Defining Variables, Local Variables, Variables
  253. @section When a Variable is ``Void''
  254. @kindex void-variable
  255. @cindex void variable
  256.  
  257.   If you have never given a symbol any value as a global variable, we
  258. say that that symbol's global value is @dfn{void}.  In other words, the
  259. symbol's value cell does not have any Lisp object in it.  If you try to
  260. evaluate the symbol, you get a @code{void-variable} error rather than
  261. a value.
  262.  
  263.   Note that a value of @code{nil} is not the same as void.  The symbol
  264. @code{nil} is a Lisp object and can be the value of a variable just as any
  265. other object can be; but it is @emph{a value}.  A void variable does not
  266. have any value.
  267.  
  268.   After you have given a variable a value, you can make it void once more
  269. using @code{makunbound}.
  270.  
  271. @defun makunbound symbol
  272. This function makes the current binding of @var{symbol} void.  This
  273. causes any future attempt to use this symbol as a variable to signal the
  274. error @code{void-variable}, unless or until you set it again.
  275.  
  276. @code{makunbound} returns @var{symbol}.
  277.  
  278. @example
  279. @group
  280. (makunbound 'x)      ; @r{Make the global value}
  281.                      ;   @r{of @code{x} void.}
  282.      @result{} x
  283. @end group
  284. @group
  285. x
  286. @error{} Symbol's value as variable is void: x
  287. @end group
  288. @end example
  289.  
  290. If @var{symbol} is locally bound, @code{makunbound} affects the most
  291. local existing binding.  This is the only way a symbol can have a void
  292. local binding, since all the constructs that create local bindings
  293. create them with values.  In this case, the voidness lasts at most as
  294. long as the binding does; when the binding is removed due to exit from
  295. the construct that made it, the previous or global binding is reexposed
  296. as usual, and the variable is no longer void unless the newly reexposed
  297. binding was void all along.
  298.  
  299. @smallexample
  300. @group
  301. (setq x 1)               ; @r{Put a value in the global binding.}
  302.      @result{} 1
  303. (let ((x 2))             ; @r{Locally bind it.}
  304.   (makunbound 'x)        ; @r{Void the local binding.}
  305.   x)
  306. @error{} Symbol's value as variable is void: x
  307. @end group
  308. @group
  309. x                        ; @r{The global binding is unchanged.}
  310.      @result{} 1
  311.  
  312. (let ((x 2))             ; @r{Locally bind it.}
  313.   (let ((x 3))           ; @r{And again.}
  314.     (makunbound 'x)      ; @r{Void the innermost-local binding.}
  315.     x))                  ; @r{And refer: it's void.}
  316. @error{} Symbol's value as variable is void: x
  317. @end group
  318.  
  319. @group
  320. (let ((x 2))
  321.   (let ((x 3))
  322.     (makunbound 'x))     ; @r{Void inner binding, then remove it.}
  323.   x)                     ; @r{Now outer @code{let} binding is visible.}
  324.      @result{} 2
  325. @end group
  326. @end smallexample
  327. @end defun
  328.  
  329.   A variable that has been made void with @code{makunbound} is
  330. indistinguishable from one that has never received a value and has
  331. always been void.
  332.  
  333.   You can use the function @code{boundp} to test whether a variable is
  334. currently void.
  335.  
  336. @defun boundp variable
  337. @code{boundp} returns @code{t} if @var{variable} (a symbol) is not void;
  338. more precisely, if its current binding is not void.  It returns
  339. @code{nil} otherwise.
  340.  
  341. @smallexample
  342. @group
  343. (boundp 'abracadabra)          ; @r{Starts out void.}
  344.      @result{} nil
  345. @end group
  346. @group
  347. (let ((abracadabra 5))         ; @r{Locally bind it.}
  348.   (boundp 'abracadabra))
  349.      @result{} t
  350. @end group
  351. @group
  352. (boundp 'abracadabra)          ; @r{Still globally void.}
  353.      @result{} nil
  354. @end group
  355. @group
  356. (setq abracadabra 5)           ; @r{Make it globally nonvoid.}
  357.      @result{} 5
  358. @end group
  359. @group
  360. (boundp 'abracadabra)
  361.      @result{} t
  362. @end group
  363. @end smallexample
  364. @end defun
  365.  
  366. @node Defining Variables, Accessing Variables, Void Variables, Variables
  367. @section Defining Global Variables
  368.  
  369.   You may announce your intention to use a symbol as a global variable
  370. with a definition, using @code{defconst} or @code{defvar}.
  371.  
  372.   In Emacs Lisp, definitions serve three purposes.  First, they inform
  373. the user who reads the code that certain symbols are @emph{intended} to be
  374. used as variables.  Second, they inform the Lisp system of these things,
  375. supplying a value and documentation.  Third, they provide information to
  376. utilities such as @code{etags} and @code{make-docfile}, which create data
  377. bases of the functions and variables in a program.
  378.  
  379.   The difference between @code{defconst} and @code{defvar} is primarily
  380. a matter of intent, serving to inform human readers of whether programs
  381. will change the variable.  Emacs Lisp does not restrict the ways in
  382. which a variable can be used based on @code{defconst} or @code{defvar}
  383. declarations.  However, it also makes a difference for initialization:
  384. @code{defconst} unconditionally initializes the variable, while
  385. @code{defvar} initializes it only if it is void.
  386.  
  387.   One would expect user option variables to be defined with
  388. @code{defconst}, since programs do not change them.  Unfortunately, this
  389. has bad results if the definition is in a library that is not preloaded:
  390. @code{defconst} would override any prior value when the library is
  391. loaded.  Users would like to be able to set the option in their init
  392. files, and override the default value given in the definition.  For this
  393. reason, user options must be defined with @code{defvar}.
  394.  
  395. @defspec defvar symbol [value [doc-string]]
  396. This special form informs a person reading your code that @var{symbol}
  397. will be used as a variable that the programs are likely to set or
  398. change.  It is also used for all user option variables except in the
  399. preloaded parts of Emacs.  Note that @var{symbol} is not evaluated;
  400. the symbol to be defined must appear explicitly in the
  401. @code{defvar}.
  402.  
  403. If @var{symbol} already has a value (i.e., it is not void), @var{value}
  404. is not even evaluated, and @var{symbol}'s value remains unchanged.  If
  405. @var{symbol} is void and @var{value} is specified, it is evaluated and
  406. @var{symbol} is set to the result.  (If @var{value} is not specified,
  407. the value of @var{symbol} is not changed in any case.)
  408.  
  409. If @var{symbol} has a buffer-local binding in the current buffer,
  410. @code{defvar} sets the default value, not the local value.
  411.  
  412. If the @var{doc-string} argument appears, it specifies the documentation
  413. for the variable.  (This opportunity to specify documentation is one of
  414. the main benefits of defining the variable.)  The documentation is
  415. stored in the symbol's @code{variable-documentation} property.  The
  416. Emacs help functions (@pxref{Documentation}) look for this property.
  417.  
  418. If the first character of @var{doc-string} is @samp{*}, it means that
  419. this variable is considered to be a user option.  This affects commands
  420. such as @code{set-variable} and @code{edit-options}.
  421.  
  422. For example, this form defines @code{foo} but does not set its value:
  423.  
  424. @example
  425. @group
  426. (defvar foo)
  427.      @result{} foo
  428. @end group
  429. @end example
  430.  
  431. The following example sets the value of @code{bar} to @code{23}, and
  432. gives it a documentation string:
  433.  
  434. @example
  435. @group
  436. (defvar bar 23
  437.   "The normal weight of a bar.")
  438.      @result{} bar
  439. @end group
  440. @end example
  441.  
  442. The following form changes the documentation string for @code{bar},
  443. making it a user option, but does not change the value, since @code{bar}
  444. already has a value.  (The addition @code{(1+ 23)} is not even
  445. performed.)
  446.  
  447. @example
  448. @group
  449. (defvar bar (1+ 23)
  450.   "*The normal weight of a bar.")
  451.      @result{} bar
  452. @end group
  453. @group
  454. bar
  455.      @result{} 23
  456. @end group
  457. @end example
  458.  
  459. Here is an equivalent expression for the @code{defvar} special form:
  460.  
  461. @example
  462. @group
  463. (defvar @var{symbol} @var{value} @var{doc-string})
  464. @equiv{}
  465. (progn
  466.   (if (not (boundp '@var{symbol}))
  467.       (setq @var{symbol} @var{value}))
  468.   (put '@var{symbol} 'variable-documentation '@var{doc-string})
  469.   '@var{symbol})
  470. @end group
  471. @end example
  472.  
  473. The @code{defvar} form returns @var{symbol}, but it is normally used
  474. at top level in a file where its value does not matter.
  475. @end defspec
  476.  
  477. @defspec defconst symbol [value [doc-string]]
  478. This special form informs a person reading your code that @var{symbol}
  479. has a global value, established here, that will not normally be changed
  480. or locally bound by the execution of the program.  The user, however,
  481. may be welcome to change it.  Note that @var{symbol} is not evaluated;
  482. the symbol to be defined must appear explicitly in the @code{defconst}.
  483.  
  484. @code{defconst} always evaluates @var{value} and sets the global value
  485. of @var{symbol} to the result, provided @var{value} is given.  If
  486. @var{symbol} has a buffer-local binding in the current buffer,
  487. @code{defconst} sets the default value, not the local value.
  488.  
  489. @strong{Please note:} don't use @code{defconst} for user option
  490. variables in libraries that are not normally loaded.  The user should be
  491. able to specify a value for such a variable in the @file{.emacs} file,
  492. so that it will be in effect if and when the library is loaded later.
  493.  
  494. Here, @code{pi} is a constant that presumably ought not to be changed
  495. by anyone (attempts by the Indiana State Legislature notwithstanding).
  496. As the second form illustrates, however, this is only advisory.
  497.  
  498. @example
  499. @group
  500. (defconst pi 3 "Pi to one place.")
  501.      @result{} pi
  502. @end group
  503. @group
  504. (setq pi 4)
  505.      @result{} pi
  506. @end group
  507. @group
  508. pi
  509.      @result{} 4
  510. @end group
  511. @end example
  512. @end defspec
  513.  
  514. @defun user-variable-p variable
  515. @cindex user option
  516. This function returns @code{t} if @var{variable} is a user option,
  517. intended to be set by the user for customization, @code{nil} otherwise.
  518. (Variables other than user options exist for the internal purposes of
  519. Lisp programs, and users need not know about them.)
  520.  
  521. User option variables are distinguished from other variables by the
  522. first character of the @code{variable-documentation} property.  If the
  523. property exists and is a string, and its first character is @samp{*},
  524. then the variable is a user option.
  525. @end defun
  526.  
  527.   Note that if the @code{defconst} and @code{defvar} special forms are
  528. used while the variable has a local binding, the local binding's value
  529. is set, and the global binding is not changed.  This would be confusing.
  530. But the normal way to use these special forms is at top level in a file,
  531. where no local binding should be in effect.
  532.  
  533. @node Accessing Variables, Setting Variables, Defining Variables, Variables
  534. @section Accessing Variable Values
  535.  
  536.   The usual way to reference a variable is to write the symbol which
  537. names it (@pxref{Symbol Forms}).  This requires you to specify the
  538. variable name when you write the program.  Usually that is exactly what
  539. you want to do.  Occasionally you need to choose at run time which
  540. variable to reference; then you can use @code{symbol-value}.
  541.  
  542. @defun symbol-value symbol
  543. This function returns the value of @var{symbol}.  This is the value in
  544. the innermost local binding of the symbol, or its global value if it
  545. has no local bindings.
  546.  
  547. @example
  548. @group
  549. (setq abracadabra 5)
  550.      @result{} 5
  551. @end group
  552. @group
  553. (setq foo 9)
  554.      @result{} 9
  555. @end group
  556.  
  557. @group
  558. ;; @r{Here the symbol @code{abracadabra}}
  559. ;;   @r{is the symbol whose value is examined.}
  560. (let ((abracadabra 'foo))
  561.   (symbol-value 'abracadabra))
  562.      @result{} foo
  563. @end group
  564.  
  565. @group
  566. ;; @r{Here the value of @code{abracadabra},}
  567. ;;   @r{which is @code{foo},}
  568. ;;   @r{is the symbol whose value is examined.}
  569. (let ((abracadabra 'foo))
  570.   (symbol-value abracadabra))
  571.      @result{} 9
  572. @end group
  573.  
  574. @group
  575. (symbol-value 'abracadabra)
  576.      @result{} 5
  577. @end group
  578. @end example
  579.  
  580. A @code{void-variable} error is signaled if @var{symbol} has neither a
  581. local binding nor a global value.
  582. @end defun
  583.  
  584. @node Setting Variables, Variable Scoping, Accessing Variables, Variables
  585. @section How to Alter a Variable Value
  586.  
  587.   The usual way to change the value of a variable is with the special
  588. form @code{setq}.  When you need to compute the choice of variable at
  589. run time, use the function @code{set}.
  590.  
  591. @defspec setq [symbol form]@dots{}
  592. This special form is the most common method of changing a variable's
  593. value.  Each @var{symbol} is given a new value, which is the result of
  594. evaluating the corresponding @var{form}.  The most-local existing
  595. binding of the symbol is changed.
  596.  
  597. The value of the @code{setq} form is the value of the last @var{form}.
  598.  
  599. @example
  600. @group
  601. (setq x (1+ 2))
  602.      @result{} 3
  603. @end group
  604. @group
  605. x                   ; @r{@code{x} now has a global value.}
  606.      @result{} 3
  607. @end group
  608. @group
  609. (let ((x 5)) 
  610.   (setq x 6)        ; @r{The local binding of @code{x} is set.}
  611.   x)
  612.      @result{} 6
  613. @end group
  614. @group
  615. x                   ; @r{The global value is unchanged.}
  616.      @result{} 3
  617. @end group
  618. @end example
  619.  
  620. Note that the first @var{form} is evaluated, then the first
  621. @var{symbol} is set, then the second @var{form} is evaluated, then the
  622. second @var{symbol} is set, and so on:
  623.  
  624. @example
  625. @group
  626. (setq x 10          ; @r{Notice that @code{x} is set before}
  627.       y (1+ x))     ;   @r{the value of @code{y} is computed.}
  628.      @result{} 11             
  629. @end group
  630. @end example
  631. @end defspec
  632.  
  633. @defun set symbol value
  634. This function sets @var{symbol}'s value to @var{value}, then
  635. returns @var{value}.  Since @code{set} is a function, the expression
  636. written for @var{symbol} is evaluated to obtain the symbol to be
  637. set.
  638.  
  639. The most-local existing binding of the variable is the binding that is
  640. set; shadowed bindings are not affected.  If @var{symbol} is not
  641. actually a symbol, a @code{wrong-type-argument} error is signaled.
  642.  
  643. @example
  644. @group
  645. (set one 1)
  646. @error{} Symbol's value as variable is void: one
  647. @end group
  648. @group
  649. (set 'one 1)
  650.      @result{} 1
  651. @end group
  652. @group
  653. (set 'two 'one)
  654.      @result{} one
  655. @end group
  656. @group
  657. (set two 2)         ; @r{@code{two} evaluates to symbol @code{one}.}
  658.      @result{} 2
  659. @end group
  660. @group
  661. one                 ; @r{So it is @code{one} that was set.}
  662.      @result{} 2
  663. (let ((one 1))      ; @r{This binding of @code{one} is set,}
  664.   (set 'one 3)      ;   @r{not the global value.}
  665.   one)
  666.      @result{} 3
  667. @end group
  668. @group
  669. one
  670.      @result{} 2
  671. @end group
  672. @end example
  673.  
  674. Logically speaking, @code{set} is a more fundamental primitive that
  675. @code{setq}.  Any use of @code{setq} can be trivially rewritten to use
  676. @code{set}; @code{setq} could even be defined as a macro, given the
  677. availability of @code{set}.  However, @code{set} itself is rarely used;
  678. beginners hardly need to know about it.  It is needed only when the
  679. choice of variable to be set is made at run time.  For example, the
  680. command @code{set-variable}, which reads a variable name from the user
  681. and then sets the variable, needs to use @code{set}.
  682.  
  683. @cindex CL note---@code{set} local
  684. @quotation
  685. @b{Common Lisp note:} in Common Lisp, @code{set} always changes the
  686. symbol's special value, ignoring any lexical bindings.  In Emacs Lisp, all
  687. variables and all bindings are special, so @code{set} always affects the
  688. most local existing binding.
  689. @end quotation
  690. @end defun
  691.  
  692. @node Variable Scoping, Buffer-Local Variables, Setting Variables, Variables
  693. @section Scoping Rules for Variable Bindings
  694.  
  695.   A given symbol @code{foo} may have several local variable bindings,
  696. established at different places in the Lisp program, as well as a global
  697. binding.  The most recently established binding takes precedence over
  698. the others.
  699.  
  700. @cindex scope
  701. @cindex extent
  702. @cindex dynamic scoping
  703.   Local bindings in Emacs Lisp have @dfn{indefinite scope} and
  704. @dfn{dynamic extent}.  @dfn{Scope} refers to @emph{where} textually in
  705. the source code the binding can be accessed.  Indefinite scope means
  706. that any part of the program can potentially access the variable
  707. binding.  @dfn{Extent} refers to @emph{when}, as the program is
  708. executing, the binding exists.  Dynamic extent means that the binding
  709. lasts as long as the activation of the construct that established it.
  710.  
  711.   The combination of dynamic extent and indefinite scope is called
  712. @dfn{dynamic scoping}.  By contrast, most programming languages use
  713. @dfn{lexical scoping}, in which references to a local variable must be
  714. textually within the function or block that binds the variable.
  715.  
  716. @cindex CL note---special variables
  717. @quotation
  718. @b{Common Lisp note:} variables declared ``special'' in Common Lisp
  719. are dynamically scoped like variables in Emacs Lisp.
  720. @end quotation
  721.  
  722. @menu
  723. * Scope::          Scope means where in the program a value is visible.
  724.                      Comparison with other languages.
  725. * Extent::         Extent means how long in time a value exists.
  726. * Impl of Scope::  Two ways to implement dynamic scoping.
  727. * Using Scoping::  How to use dynamic scoping carefully and avoid problems.
  728. @end menu
  729.  
  730. @node Scope, Extent, Variable Scoping, Variable Scoping
  731. @subsection Scope
  732.  
  733.   Emacs Lisp uses @dfn{indefinite scope} for local variable bindings.
  734. This means that any function anywhere in the program text might access a
  735. given binding of a variable.  Consider the following function
  736. definitions:
  737.  
  738. @example
  739. @group
  740. (defun binder (x)   ; @r{@code{x} is bound in @code{binder}.}
  741.    (foo 5))         ; @r{@code{foo} is some other function.}
  742. @end group
  743.  
  744. @group
  745. (defun user ()      ; @r{@code{x} is used in @code{user}.}
  746.   (list x))
  747. @end group
  748. @end example
  749.  
  750.   In a lexically scoped language, the binding of @code{x} from
  751. @code{binder} would never be accessible in @code{user}, because
  752. @code{user} is not textually contained within the function
  753. @code{binder}.  However, in dynamically scoped Emacs Lisp, @code{user}
  754. may or may not refer to the binding of @code{x} established in
  755. @code{binder}, depending on circumstances:
  756.  
  757. @itemize @bullet
  758. @item
  759. If we call @code{user} directly without calling @code{binder} at all,
  760. then whatever binding of @code{x} is found, it cannot come from
  761. @code{binder}.
  762.  
  763. @item
  764. If we define @code{foo} as follows and call @code{binder}, then the
  765. binding made in @code{binder} will be seen in @code{user}:
  766.  
  767. @example
  768. @group
  769. (defun foo (lose)
  770.   (user))
  771. @end group
  772. @end example
  773.  
  774. @item
  775. If we define @code{foo} as follows and call @code{binder}, then the
  776. binding made in @code{binder} @emph{will not} be seen in @code{user}:
  777.  
  778. @example
  779. (defun foo (x)
  780.   (user))
  781. @end example
  782.  
  783. @noindent
  784. Here, when @code{foo} is called by @code{binder}, it binds @code{x}.
  785. (The binding in @code{foo} is said to @dfn{shadow} the one made in
  786. @code{binder}.)  Therefore, @code{user} will access the @code{x} bound
  787. by @code{foo} instead of the one bound by @code{binder}.
  788. @end itemize
  789.  
  790. @node Extent, Impl of Scope, Scope, Variable Scoping
  791. @subsection Extent
  792.  
  793.   @dfn{Extent} refers to the time during program execution that a
  794. variable name is valid.  In Emacs Lisp, a variable is valid only while
  795. the form that bound it is executing.  This is called @dfn{dynamic
  796. extent}.  ``Local'' or ``automatic'' variables in most languages,
  797. including C and Pascal, have dynamic extent.
  798.  
  799.   One alternative to dynamic extent is @dfn{indefinite extent}.  This
  800. means that a variable binding can live on past the exit from the form
  801. that made the binding.  Common Lisp and Scheme, for example, support
  802. this, but Emacs Lisp does not.
  803.  
  804.   To illustrate this, the function below, @code{make-add}, returns a
  805. function that purports to add @var{n} to its own argument @var{m}.
  806. This would work in Common Lisp, but it does not work as intended in
  807. Emacs Lisp, because after the call to @code{make-add} exits, the
  808. variable @code{n} is no longer bound to the actual argument 2.
  809.  
  810. @example
  811. (defun make-add (n)
  812.     (function (lambda (m) (+ n m))))  ; @r{Return a function.}
  813.      @result{} make-add
  814. (fset 'add2 (make-add 2))  ; @r{Define function @code{add2}}
  815.                            ;   @r{with @code{(make-add 2)}.}
  816.      @result{} (lambda (m) (+ n m))
  817. (add2 4)                   ; @r{Try to add 2 to 4.}
  818. @error{} Symbol's value as variable is void: n
  819. @end example
  820.  
  821. @node Impl of Scope, Using Scoping, Extent, Variable Scoping
  822. @subsection Implementation of Dynamic Scoping
  823. @cindex deep binding
  824.  
  825.   A simple sample implementation (which is not how Emacs Lisp actually
  826. works) may help you understand dynamic binding.  This technique is
  827. called @dfn{deep binding} and was used in early Lisp systems.
  828.  
  829.   Suppose there is a stack of bindings: variable-value pairs.  At entry
  830. to a function or to a @code{let} form, we can push bindings on the stack
  831. for the arguments or local variables created there.  We can pop those
  832. bindings from the stack at exit from the binding construct.
  833.  
  834.   We can find the value of a variable by searching the stack from top to
  835. bottom for a binding for that variable; the value from that binding is
  836. the value of the variable.  To set the variable, we search for the
  837. current binding, then store the new value into that binding.
  838.  
  839.   As you can see, a function's bindings remain in effect as long as it
  840. continues execution, even during its calls to other functions.  That is
  841. why we say the extent of the binding is dynamic.  And any other function
  842. can refer to the bindings, if it uses the same variables while the
  843. bindings are in effect.  That is why we say the scope is indefinite.
  844.  
  845. @cindex shallow binding
  846.   The actual implementation of variable scoping in GNU Emacs Lisp uses a
  847. technique called @dfn{shallow binding}.  Each variable has a standard
  848. place in which its current value is always found---the value cell of the
  849. symbol.
  850.  
  851.   In shallow binding, setting the variable works by storing a value in
  852. the value cell.  When a new local binding is created, the local value is
  853. stored in the value cell, and the old value (belonging to a previous
  854. binding) is pushed on a stack.  When a binding is eliminated, the old
  855. value is popped off the stack and stored in the value cell.
  856.  
  857.   We use shallow binding because it has the same results as deep
  858. binding, but runs faster, since there is never a need to search for a
  859. binding.
  860.  
  861. @node Using Scoping,, Impl of Scope, Variable Scoping
  862. @subsection Proper Use of Dynamic Scoping
  863.  
  864.   Binding a variable in one function and using it in another is a
  865. powerful technique, but if used without restraint, it can make programs
  866. hard to understand.  There are two clean ways to use this technique:
  867.  
  868. @itemize @bullet
  869. @item
  870. Use or bind the variable only in a few related functions, written close
  871. together in one file.  Such a variable is used for communication within
  872. one program.
  873.  
  874. You should write comments to inform other programmers that they can see
  875. all uses of the variable before them, and to advise them not to add uses
  876. elsewhere.
  877.  
  878. @item
  879. Give the variable a well-defined, documented meaning, and make all
  880. appropriate functions refer to it (but not bind it or set it) wherever
  881. that meaning is relevant.  For example, the variable
  882. @code{case-fold-search} is defined as ``non-@code{nil} means ignore case
  883. when searching''; various search and replace functions refer to it
  884. directly or through their subroutines, but do not bind or set it.
  885.  
  886. Then you can bind the variable in other programs, knowing reliably what
  887. the effect will be.
  888. @end itemize
  889.  
  890. @node Buffer-Local Variables,, Variable Scoping, Variables
  891. @section Buffer-Local Variables
  892. @cindex variables, buffer-local
  893. @cindex buffer-local variables
  894.  
  895.   Global and local variable bindings are found in most programming
  896. languages in one form or another.  Emacs also supports another, unusual
  897. kind of variable binding: @dfn{buffer-local} bindings, which apply only
  898. to one buffer.  Emacs Lisp is meant for programming editing commands,
  899. and having different values for a variable in different buffers is an
  900. important customization method.
  901.  
  902. @menu
  903. * Intro to Buffer-Local::      Introduction and concepts.
  904. * Creating Buffer-Local::      Creating and destroying buffer-local bindings.
  905. * Default Value::              The default value is seen in buffers
  906.                                  that don't have their own local values.
  907. @end menu
  908.  
  909. @node Intro to Buffer-Local, Creating Buffer-Local, Buffer-Local Variables, Buffer-Local Variables
  910. @subsection Introduction to Buffer-Local Variables
  911.  
  912.   A buffer-local variable has a buffer-local binding associated with a
  913. particular buffer.  The binding is in effect when that buffer is
  914. current; otherwise, it is not in effect.  If you set the variable while
  915. a buffer-local binding is in effect, the new value goes in that binding,
  916. so the global binding is unchanged; this means that the change is
  917. visible in that buffer alone.
  918.  
  919.   A variable may have buffer-local bindings in some buffers but not in
  920. others.  The global binding is shared by all the buffers that don't have
  921. their own bindings.  Thus, if you set the variable in a buffer that does
  922. not have a buffer-local binding for it, the new value is visible in all
  923. buffers except those with buffer-local bindings.  (Here we are assuming
  924. that there are no @code{let}-style local bindings to complicate the issue.)
  925.  
  926.   The most common use of buffer-local bindings is for major modes to change
  927. variables that control the behavior of commands.  For example, C mode and
  928. Lisp mode both set the variable @code{paragraph-start} to specify that only
  929. blank lines separate paragraphs.  They do this by making the variable
  930. buffer-local in the buffer that is being put into C mode or Lisp mode, and
  931. then setting it to the new value for that mode.
  932.  
  933.   The usual way to make a buffer-local binding is with
  934. @code{make-local-variable}, which is what major mode commands use.  This
  935. affects just the current buffer; all other buffers (including those yet to
  936. be created) continue to share the global value.
  937.  
  938. @cindex automatically buffer-local
  939.   A more powerful operation is to mark the variable as
  940. @dfn{automatically buffer-local} by calling
  941. @code{make-variable-buffer-local}.  You can think of this as making the
  942. variable local in all buffers, even those yet to be created.  More
  943. precisely, the effect is that setting the variable automatically makes
  944. the variable local to the current buffer if it is not already so.  All
  945. buffers start out by sharing the global value of the variable as usual,
  946. but any @code{setq} creates a buffer-local binding for the current
  947. buffer.  The new value is stored in the buffer-local binding, leaving
  948. the (default) global binding untouched.  The global value can no longer
  949. be changed with @code{setq}; you need to use @code{setq-default} to do
  950. that.
  951.  
  952.   @strong{Warning:} when a variable has local values in one or more
  953. buffers, you can get Emacs very confused by binding the variable with
  954. @code{let}, changing to a different current buffer in which a different
  955. binding is in effect, and then exiting the @code{let}.  To preserve your
  956. sanity, it is wise to avoid such situations.  If you use
  957. @code{save-excursion} around each piece of code that changes to a
  958. different current buffer, you will not have this problem.  Here is an
  959. example of incorrect code:
  960.  
  961. @example
  962. @group
  963. (setq foo 'b)
  964. (set-buffer "a")
  965. (make-local-variable 'foo)
  966. @end group
  967. @group
  968. (setq foo 'a)
  969. (let ((foo 'temp))
  970.   (set-buffer "b")
  971.   @dots{})
  972. foo @result{} 'a      ; @r{The old buffer-local value from buffer @samp{a}}
  973.                ;   @r{is now the default value.}
  974. @end group
  975. @group
  976. (set-buffer "a")
  977. foo @result{} 'temp   ; @r{The local value that should be gone}
  978.                ;   @r{is now the buffer-local value in buffer @samp{a}.}
  979. @end group
  980. @end example
  981.  
  982. @noindent
  983. But @code{save-excursion} as shown here avoids the problem:
  984.  
  985. @example
  986. @group
  987. (let ((foo 'temp))
  988.   (save-excursion
  989.     (set-buffer "b")
  990.     @dots{}))
  991. @end group
  992. @end example
  993.  
  994.   Local variables in a file you edit are also represented by
  995. buffer-local bindings for the buffer that holds the file within Emacs.
  996. @xref{Auto Major Mode}.
  997.  
  998. @node Creating Buffer-Local, Default Value, Intro to Buffer-Local, Buffer-Local Variables
  999. @subsection Creating and Destroying Buffer-local Bindings
  1000.  
  1001. @deffn Command make-local-variable variable
  1002. This function creates a buffer-local binding in the current buffer for
  1003. @var{variable} (a symbol).  Other buffers are not affected.  The value
  1004. returned is @var{variable}.
  1005.  
  1006. @c Emacs 19 feature
  1007. The buffer-local value of @var{variable} starts out as the same value
  1008. @var{variable} previously had.  If @var{variable} was void, it remains
  1009. void.
  1010.  
  1011. @example
  1012. @group
  1013. ;; @r{In buffer @samp{b1}:}
  1014. (setq foo 5)                ; @r{Affects all buffers.}
  1015.      @result{} 5
  1016. @end group
  1017. @group
  1018. (make-local-variable 'foo)  ; @r{Now it is local in @samp{b1}.}
  1019.      @result{} foo
  1020. @end group
  1021. @group
  1022. foo                         ; @r{That did not change}
  1023.      @result{} 5                   ;   @r{the value.}
  1024. @end group
  1025. @group
  1026. (setq foo 6)                ; @r{Change the value}
  1027.      @result{} 6                   ;   @r{in @samp{b1}.}
  1028. @end group
  1029. @group
  1030. foo
  1031.      @result{} 6
  1032. @end group
  1033.  
  1034. @group
  1035. ;; @r{In buffer @samp{b2}, the value hasn't changed.}
  1036. (save-excursion
  1037.   (set-buffer "b2")
  1038.   foo)
  1039.      @result{} 5
  1040. @end group
  1041. @end example
  1042. @end deffn
  1043.  
  1044. @deffn Command make-variable-buffer-local variable
  1045. This function marks @var{variable} (a symbol) automatically
  1046. buffer-local, so that any attempt to set it will make it local to the
  1047. current buffer at the time.
  1048.  
  1049. The value returned is @var{variable}.
  1050. @end deffn
  1051.  
  1052. @defun buffer-local-variables &optional buffer
  1053. This function tells you what the buffer-local variables are in buffer
  1054. @var{buffer}.  It returns an association list (@pxref{Association
  1055. Lists}) in which each association contains one buffer-local variable and
  1056. its value.  If @var{buffer} is omitted, the current buffer is used.
  1057.  
  1058. @example
  1059. @group
  1060. (setq lcl (buffer-local-variables))
  1061. @result{} ((fill-column . 75)
  1062.     (case-fold-search . t)
  1063.     @dots{}
  1064.     (mark-ring #<marker at 5454 in buffers.texi>)
  1065.     (require-final-newline . t))
  1066. @end group
  1067. @end example
  1068.  
  1069. Note that storing new values into the @sc{cdr}s of the elements in this
  1070. list does @emph{not} change the local values of the variables.
  1071. @end defun
  1072.  
  1073. @deffn Command kill-local-variable variable
  1074. This function deletes the buffer-local binding (if any) for
  1075. @var{variable} (a symbol) in the current buffer.  As a result, the
  1076. global (default) binding of @var{variable} becomes visible in this
  1077. buffer.  Usually this results in a change in the value of
  1078. @var{variable}, since the global value is usually different from the
  1079. buffer-local value just eliminated.
  1080.  
  1081. It is possible to kill the local binding of a variable that automatically
  1082. becomes local when set.  This causes the variable to show its global value
  1083. in the current buffer.  However, if you set the variable again, this will
  1084. once again create a local value.
  1085.  
  1086. @code{kill-local-variable} returns @var{variable}.
  1087. @end deffn
  1088.  
  1089. @defun kill-all-local-variables
  1090. This function eliminates all the buffer-local variable bindings of the
  1091. current buffer except for variables marker as ``permanent''.  As a
  1092. result, the buffer will see the default values of most variables.
  1093.  
  1094. This function also resets certain other information pertaining to the
  1095. buffer: its local keymap is set to @code{nil}, its syntax table is set
  1096. to the value of @code{standard-syntax-table}, and its abbrev table is
  1097. set to the value of @code{fundamental-mode-abbrev-table}.
  1098.  
  1099. Every major mode command begins by calling this function, which has the
  1100. effect of switching to Fundamental mode and erasing most of the effects
  1101. of the previous major mode.  To ensure that this does its job, the
  1102. variables that major modes set should not be marked permanent.
  1103.  
  1104. @code{kill-all-local-variables} returns @code{nil}.
  1105. @end defun
  1106.  
  1107. @c Emacs 19 feature
  1108. @cindex permanent local variable
  1109. A local variable is @dfn{permanent} if the variable name (a symbol) has a
  1110. @code{permanent-local} property that is non-@code{nil}.  Permanent
  1111. locals are appropriate for data pertaining to where the file came from
  1112. or how to save it, rather than with how to edit the contents.
  1113.  
  1114. @node Default Value,, Creating Buffer-Local, Buffer-Local Variables
  1115. @subsection The Default Value of a Buffer-Local Variable
  1116. @cindex default value
  1117.  
  1118.   The global value of a variable with buffer-local bindings is also
  1119. called the @dfn{default} value, because it is the value that is in
  1120. effect except when specifically overridden.
  1121.  
  1122.   The functions @code{default-value} and @code{setq-default} allow you
  1123. to access and change the default value regardless of whether the current
  1124. buffer has a buffer-local binding.  For example, you could use
  1125. @code{setq-default} to change the default setting of
  1126. @code{paragraph-start} for most buffers; and this would work even when
  1127. you are in a C or Lisp mode buffer which has a buffer-local value for
  1128. this variable.
  1129.  
  1130. @c Emacs 19 feature
  1131.   The special forms @code{defvar} and @code{defconst} also set the
  1132. default value (if they set the variable at all), rather than any local
  1133. value.
  1134.  
  1135. @defun default-value symbol
  1136. This function returns @var{symbol}'s default value.  This is the value
  1137. that is seen in buffers that do not have their own values for this
  1138. variable.  If @var{symbol} is not buffer-local, this is equivalent to
  1139. @code{symbol-value} (@pxref{Accessing Variables}).
  1140. @end defun
  1141.  
  1142. @c Emacs 19 feature
  1143. @defun default-boundp variable
  1144. The function @code{default-boundp} tells you whether @var{variable}'s
  1145. default value is nonvoid.  If @code{(default-boundp 'foo)} returns
  1146. @code{nil}, then @code{(default-value 'foo)} would get an error.
  1147.  
  1148. @code{default-boundp} is to @code{default-value} as @code{boundp} is to
  1149. @code{symbol-value}.
  1150. @end defun
  1151.  
  1152. @defspec setq-default symbol value
  1153. This sets the default value of @var{symbol} to @var{value}.
  1154. @var{symbol} is not evaluated, but @var{value} is.  The value of the
  1155. @code{setq-default} form is @var{value}.
  1156.  
  1157. If a @var{symbol} is not buffer-local for the current buffer, and is not
  1158. marked automatically buffer-local, this has the same effect as
  1159. @code{setq}.  If @var{symbol} is buffer-local for the current buffer,
  1160. then this changes the value that other buffers will see (as long as they
  1161. don't have a buffer-local value), but not the value that the current
  1162. buffer sees.
  1163.  
  1164. @example
  1165. @group
  1166. ;; @r{In buffer @samp{foo}:}
  1167. (make-local-variable 'local)
  1168.      @result{} local
  1169. @end group
  1170. @group
  1171. (setq local 'value-in-foo)
  1172.      @result{} value-in-foo
  1173. @end group
  1174. @group
  1175. (setq-default local 'new-default)
  1176.      @result{} new-default
  1177. @end group
  1178. @group
  1179. local
  1180.      @result{} value-in-foo
  1181. @end group
  1182. @group
  1183. (default-value 'local)
  1184.      @result{} new-default
  1185. @end group
  1186.  
  1187. @group
  1188. ;; @r{In (the new) buffer @samp{bar}:}
  1189. local
  1190.      @result{} new-default
  1191. @end group
  1192. @group
  1193. (default-value 'local)
  1194.      @result{} new-default
  1195. @end group
  1196. @group
  1197. (setq local 'another-default)
  1198.      @result{} another-default
  1199. @end group
  1200. @group
  1201. (default-value 'local)
  1202.      @result{} another-default
  1203. @end group
  1204.  
  1205. @group
  1206. ;; @r{Back in buffer @samp{foo}:}
  1207. local
  1208.      @result{} value-in-foo
  1209. (default-value 'local)
  1210.      @result{} another-default
  1211. @end group
  1212. @end example
  1213. @end defspec
  1214.  
  1215. @defun set-default symbol value
  1216. This function is like @code{setq-default}, except that @var{symbol} is
  1217. evaluated.
  1218.  
  1219. @example
  1220. @group
  1221. (set-default (car '(a b c)) 23)
  1222.      @result{} 23
  1223. @end group
  1224. @group
  1225. (default-value 'a)
  1226.      @result{} 23
  1227. @end group
  1228. @end example
  1229. @end defun
  1230.  
  1231.